/** 
	@file ldapauth_TabCtrl.cpp
	@brief Handler code to draw and process tabbed controls
*/
/*
pGina ldapAuth code - based upon skeleton code for pGina plugin development
Copyright (C) 2002 Micah Cooper

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

Email: coopermj@muohio.edu
Snail Mail:
	Micah Cooper
	212A Kreger Hall
	Miami University
	Oxford OH  45056
*/
// MyTabCtrl.cpp : implementation file
//
/////////////////////////////////////////////////////
// This class is provided as is and Ben Hill takes no
// responsibility for any loss of any kind in connection
// to this code.
/////////////////////////////////////////////////////
// Is is meant purely as a educational tool and may
// contain bugs.
/////////////////////////////////////////////////////
// ben@shido.fsnet.co.uk
// http://www.shido.fsnet.co.uk
/////////////////////////////////////////////////////
// Thanks to a mystery poster in the C++ forum on 
// www.codeguru.com I can't find your name to say thanks
// for your Control drawing code. If you are that person 
// thank you very much. I have been able to use some of 
// you ideas to produce this sample application.
/////////////////////////////////////////////////////

#include "stdafx.h"
//#include "MyTabExample.h"
#include "ldapauth_TabCtrl.h"

#include "ConfigLDAP.h"
#include "ConfigUser.h"
#include "ConfigPwd.h"
#include "ConfigHooks.h"
#include "ConfigAbout.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// ldapAuthTabCtrl

ldapAuthTabCtrl::ldapAuthTabCtrl()
{
	m_tabPages[0]=new CConfigLDAP;
	m_tabPages[1]=new CConfigUser;
	m_tabPages[2]=new CConfigPwd;
	m_tabPages[3]=new CConfigHooks;
	m_tabPages[4]=new CConfigAbout;

	m_nNumberOfPages=5;
}

ldapAuthTabCtrl::~ldapAuthTabCtrl()
{
	for(int nCount=0; nCount < m_nNumberOfPages; nCount++){
		delete m_tabPages[nCount];
	}
}

void ldapAuthTabCtrl::Init()
{
	m_tabCurrent=0;

	m_tabPages[0]->Create(IDD_LDAP_TAB, this);
	m_tabPages[1]->Create(IDD_USER_TAB, this);
	m_tabPages[2]->Create(IDD_PWD_TAB, this);
	m_tabPages[3]->Create(IDD_HOOK_TAB, this);
	m_tabPages[4]->Create(IDD_ABOUT_TAB, this);

	m_tabPages[0]->ShowWindow(SW_SHOW);
	m_tabPages[1]->ShowWindow(SW_HIDE);
	m_tabPages[2]->ShowWindow(SW_HIDE);
	m_tabPages[3]->ShowWindow(SW_HIDE);
	m_tabPages[4]->ShowWindow(SW_HIDE);

	SetRectangle();
}

void ldapAuthTabCtrl::SetRectangle()
{
	CRect tabRect, itemRect;
	int nX, nY, nXc, nYc;

	GetClientRect(&tabRect);
	GetItemRect(0, &itemRect);

	nX=itemRect.left;
	nY=itemRect.bottom+1;
	nXc=tabRect.right-itemRect.left-1;
	nYc=tabRect.bottom-nY-1;

	m_tabPages[0]->SetWindowPos(&wndTop, nX, nY, nXc, nYc, SWP_SHOWWINDOW);
	for(int nCount=1; nCount < m_nNumberOfPages; nCount++){
		m_tabPages[nCount]->SetWindowPos(&wndTop, nX, nY, nXc, nYc, SWP_HIDEWINDOW);
	}
}

BEGIN_MESSAGE_MAP(ldapAuthTabCtrl, CTabCtrl)
	//{{AFX_MSG_MAP(ldapAuthTabCtrl)
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// ldapAuthTabCtrl message handlers

void ldapAuthTabCtrl::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CTabCtrl::OnLButtonDown(nFlags, point);

	if(m_tabCurrent != GetCurFocus()){
		m_tabPages[m_tabCurrent]->ShowWindow(SW_HIDE);
		m_tabCurrent=GetCurFocus();
		m_tabPages[m_tabCurrent]->ShowWindow(SW_SHOW);
		m_tabPages[m_tabCurrent]->SetFocus();
	}
}


void ldapAuthTabCtrl::OnSave()
{
	((CConfigLDAP *)m_tabPages[0])->OnSave();
	((CConfigUser *)m_tabPages[1])->OnSave();
	((CConfigPwd *)m_tabPages[2])->OnSave();
	((CConfigHooks *)m_tabPages[3])->OnSave();
}

